package  com.syqc.sensor.oil

import androidx.core.view.GravityCompat
import androidx.core.view.isVisible
import com.google.android.material.tabs.TabLayout
import com.gyf.immersionbar.ktx.immersionBar
import com.syqc.comlib.base.BaseActivity
import com.syqc.comlib.rx.filterFast
import com.syqc.utils.Keys
import com.syqc.interfaces.SimpleTabLayoutListener
import com.syqc.monitor.R
import com.syqc.monitor.databinding.ActivityOilInfo2Binding
import com.syqc.monitor.databinding.IncludeTabLayoutBinding
import com.syqc.utils.DataUtil

class OilInfoActivity : BaseActivity<ActivityOilInfo2Binding>(ActivityOilInfo2Binding::inflate) {
    private lateinit var plate: String
    private val mFragments by lazy { ArrayList<OilInfoFragment>() }

    private lateinit var mTabVB: IncludeTabLayoutBinding
    private lateinit var carId: String
    private lateinit var teamName: String


    override fun initView() {
        immersionBar { titleBar(binding.listToolbar) }
        mTabVB = IncludeTabLayoutBinding.bind(binding.root)
        plate = intent.getStringExtra(Keys.INTENT_TITLE) ?: ""
        carId = intent.getStringExtra(Keys.INTENT_OTHER) ?: ""
        teamName = intent.getStringExtra(Keys.INTENT_TEAM_NAME) ?: ""
        binding.tvToolbarTitle.text = plate

        val dayStats = DataUtil.dayStats
        dayStats?.forEachIndexed { index, statsType ->
            mFragments.add(OilInfoFragment.getInstance(plate, carId, teamName, index))
            mTabVB.tabLayout.addTab(mTabVB.tabLayout.newTab())
            mTabVB.tabLayout.getTabAt(index)?.text = statsType.name
        }
        if (mFragments.size > 0)
            showFragment(mFragments[0])
        if ((dayStats?.size ?: 0) < 2) {
            mTabVB.tabLayout.isVisible = false
        }
        binding.cbDate.isSelected = OilModel.getDate()
        binding.cbTeam.isSelected = OilModel.getTeam()
        binding.cbAdd.isSelected = OilModel.getUp()
        binding.cbLeak.isSelected = OilModel.getLeak()
        binding.cbDown.isSelected = OilModel.getDown()
        binding.cbRun.isSelected = OilModel.getRun()
        binding.cbSpeed.isSelected = OilModel.getSpeed()
        binding.cbMile.isSelected = OilModel.getMile()
        binding.cbRun100.isSelected = OilModel.getRun100()
        binding.cbTotal100.isSelected = OilModel.getTotal100()
    }

    private var currentFragment: OilInfoFragment? = null
    private fun showFragment(fragment: OilInfoFragment) {
        if (currentFragment != null && currentFragment == fragment) return
        supportFragmentManager.beginTransaction().apply {
            currentFragment?.let { hide(it) }
            currentFragment = fragment
            if (fragment.isAdded) {
                show(fragment)
            } else {
                add(R.id.content, fragment).show(fragment)
            }
        }.commit()
    }


    override fun setListener() {
        mTabVB.tabLayout.addOnTabSelectedListener(object : SimpleTabLayoutListener {
            override fun onTabSelected(tab: TabLayout.Tab?) {
                showFragment(mFragments[tab?.position ?: 0])
            }
        })
        binding.ivToolbarStart.setOnClickListener {
            finish()
        }

        binding.ivToolbarEnd.setOnClickListener {
            if (binding.listDrawerLayout.isDrawerOpen(GravityCompat.END)) {
                binding.listDrawerLayout.closeDrawer(GravityCompat.END)
            } else {
                binding.listDrawerLayout.openDrawer(GravityCompat.END)
            }
        }
        binding.cbDate.filterFast {
            binding.cbDate.isSelected = !OilModel.getDate()
            OilModel.setDate(binding.cbDate.isSelected)
            refreshFragments()
        }

        binding.cbTeam.filterFast {
            binding.cbTeam.isSelected = !OilModel.getTeam()
            OilModel.setTeam(binding.cbTeam.isSelected)
            refreshFragments()
        }

        binding.cbAdd.filterFast {
            binding.cbAdd.isSelected = !OilModel.getUp()
            OilModel.setUp(binding.cbAdd.isSelected)
            refreshFragments()
        }

        binding.cbLeak.filterFast {
            binding.cbLeak.isSelected = !OilModel.getLeak()
            OilModel.setLeak(binding.cbLeak.isSelected)
            refreshFragments()
        }

        binding.cbDown.filterFast {
            binding.cbDown.isSelected = !OilModel.getDown()
            OilModel.setDown(binding.cbDown.isSelected)
            refreshFragments()
        }

        binding.cbRun.filterFast {
            binding.cbRun.isSelected = !OilModel.getRun()
            OilModel.setRun(binding.cbRun.isSelected)
            refreshFragments()
        }

        binding.cbSpeed.filterFast {
            binding.cbSpeed.isSelected = !OilModel.getSpeed()
            OilModel.setSpeed(binding.cbSpeed.isSelected)
            refreshFragments()
        }

        binding.cbMile.filterFast {
            binding.cbMile.isSelected = !OilModel.getMile()
            OilModel.setMile(binding.cbMile.isSelected)
            refreshFragments()
        }

        binding.cbRun100.filterFast {
            binding.cbRun100.isSelected = !OilModel.getRun100()
            OilModel.setRun100(binding.cbRun100.isSelected)
            refreshFragments()
        }

        binding.cbTotal100.filterFast {
            binding.cbTotal100.isSelected = !OilModel.getTotal100()
            OilModel.setTotal100(binding.cbTotal100.isSelected)
            refreshFragments()
        }


    }

    /**
     * 刷新所有
     */
    private fun refreshFragments() {
        mFragments.forEach { it.refresh() }
    }


    override fun onDestroy() {
        super.onDestroy()
        currentFragment = null
        DataUtil.dayStats = null
    }
}